public class Solution43 {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int m = num1.length(), n = num2.length();
        int[] ansArr = new int[m + n];

        for (int i = num1.length() - 1; i >= 0; i--) {
            int x = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
                int y = num2.charAt(j) - '0';
                ansArr[m - i + n - j - 2] += x * y;
            }
        }
        StringBuilder ans = new StringBuilder();

        for (int i = 0; i < ansArr.length - 1; i++) {
            if (ansArr[i] >= 10) {
                ansArr[i + 1] += ansArr[i] / 10;
                ansArr[i] %= 10;
            }
            ans.append(ansArr[i]);
        }
        if (ansArr[ansArr.length - 1] != 0) {
            ans.append(ansArr[ansArr.length - 1]);
        }
        return ans.reverse().toString();
    }
}
